package com.definesys.smecrailtransit.module.MonthForm.PopWindowAdapter;

import android.animation.ValueAnimator;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.base.SmecBaseRecyclerViewHolder;
import com.definesys.smecrailtransit.databinding.LayoutPopwindowDoubleItemBinding;
import com.definesys.smecrailtransit.databinding.LayoutPopwindowItemBinding;
import com.definesys.smecrailtransit.module.MonthForm.MonthFormPresenter;
import com.definesys.smecrailtransit.module.MonthForm.bean.PositionBean;
import com.definesys.smecrailtransit.module.MonthForm.dialog.MonthFormDialog;
import com.hwangjr.rxbus.Bus;


import java.util.ArrayList;

/**
 * Created by cjy on 2017/7/28.
 */

public class PopWindowAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {
    private ArrayList<PositionBean> list;
    private int maxNumber1;
    private int maxNumber2;
    private DisplayMetrics dm;
    protected Bus mRxbus;
    private MonthFormPresenter monthFormPresenter;
    public boolean showAnimation;
    public MonthFormDialog monthFormDialog;

    public PopWindowAdapter(DisplayMetrics dm, Bus mRxbus, MonthFormPresenter monthFormPresenter,MonthFormDialog dialog){

        this.list = new ArrayList<>();
        this.dm = dm;
        this.mRxbus = mRxbus;
        this.monthFormPresenter = monthFormPresenter;
        this.showAnimation = true;
        monthFormDialog=dialog;
    }

    public void setList(ArrayList<PositionBean> list){
        if(list != null && list.size() != 0) {
            this.list = list;
            this.maxNumber1 = this.list.get(0).getFtValue();
            for (int i = 1; i < this.list.size(); i++) {
                if (maxNumber1 < this.list.get(i).getFtValue()) {
                    maxNumber1 = this.list.get(i).getFtValue();
                }
            }

            this.maxNumber2 = this.list.get(0).getNotselfValue();
            for (int i = 1; i < this.list.size(); i++) {
                if (maxNumber2 < this.list.get(i).getNotselfValue()) {
                    maxNumber2 = this.list.get(i).getNotselfValue();
                }
            }
        }
        else{
            this.list = new ArrayList<>();
        }
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutPopwindowDoubleItemBinding layoutPopwindowDoubleItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                , R.layout.layout_popwindow_double_item,parent,false);
        SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                SmecBaseRecyclerViewHolder(layoutPopwindowDoubleItemBinding.getRoot());
        smecBaseRecyclerViewHolder.setViewType(viewType);
        smecBaseRecyclerViewHolder.setBinding(layoutPopwindowDoubleItemBinding);
        return smecBaseRecyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, int position) {
        LayoutPopwindowDoubleItemBinding viewDataBinding=(LayoutPopwindowDoubleItemBinding)holder.getBinding();
        viewDataBinding.text.setText(list.get(position).getEleCode());
        viewDataBinding.number1.setText(String.valueOf(list.get(position).getFtValue())+"次");
        viewDataBinding.number2.setText(String.valueOf(list.get(position).getNotselfValue())+"次");

        if(maxNumber1 != 0){
            int width = this.dm.widthPixels*1/4*this.list.get(position).getFtValue()/this.maxNumber1;
            if(showAnimation){
                //属性动画对象
                ValueAnimator va ;
                //显示view，宽度从0变到width值
                va = ValueAnimator.ofInt(0,width);
                va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        //获取当前的height值
                        int w =(Integer)valueAnimator.getAnimatedValue();
                        //动态更新view的高度
                        viewDataBinding.pillar1.getLayoutParams().width = w;
                        viewDataBinding.pillar1.requestLayout();
                    }
                });
                va.setDuration(800);
                //开始动画
                va.start();
            }else{
                viewDataBinding.pillar1.getLayoutParams().width = width;
            }
        }else{
            viewDataBinding.pillar1.getLayoutParams().width = 0;
        }

        if(maxNumber2 != 0){
            int width = this.dm.widthPixels*1/4*this.list.get(position).getNotselfValue()/this.maxNumber2;
            if(showAnimation){
                //属性动画对象
                ValueAnimator va ;
                //显示view，宽度从0变到width值
                va = ValueAnimator.ofInt(0,width);
                va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        //获取当前的height值
                        int w =(Integer)valueAnimator.getAnimatedValue();
                        //动态更新view的高度
                        viewDataBinding.pillar2.getLayoutParams().width = w;
                        viewDataBinding.pillar2.requestLayout();
                    }
                });
                va.setDuration(800);
                //开始动画
                va.start();
            }else{
                viewDataBinding.pillar2.getLayoutParams().width = width;
            }
        }else{
            viewDataBinding.pillar2.getLayoutParams().width = 0;
        }
        viewDataBinding.item.setOnClickListener(v -> mRxbus.post(MonthFormDialog.SHOW_POP_DETAIL,list.get(position)));
        viewDataBinding.invalidateAll();
    }

    @Override
    public int getItemCount() {
        return this.list.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }
}
